﻿using System;
using System.Web;
using Ext.Net;
using System.Collections.Generic;
using System.Collections;
using Film.Repository;


namespace PhuongDoAn.Admin.Country.Handler
{
    /// <summary>
    /// Summary description for ListHandler
    /// </summary>
    public class ListHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string query = "";
            int start = 0;
            int limit = 25;
            int count = 0;

            if (!string.IsNullOrEmpty(context.Request["query"]))
            {
                query = context.Request["query"];
            }
            if (!string.IsNullOrEmpty(context.Request["start"]))
            {
                int.TryParse(context.Request["start"], out start);
            }

            if (!string.IsNullOrEmpty(context.Request["limit"]))
            {
                int.TryParse(context.Request["limit"], out limit);
            }
            List<tblCountry> result = new List<tblCountry>();
            tblCountryRepository countryRepository = new tblCountryRepository();
            result = countryRepository.Filter(query, start, limit, out count);

            List<tblCountryHandler> listHandler = result.ConvertAll<tblCountryHandler>(delegate(tblCountry e)
            {
                return new tblCountryHandler() { CountryID = e.CountryID, CountryName = e.CountryName };
            });
            context.Response.Write(string.Format("{{'Data':{0},'TotalRecords':{1}}}", JSON.Serialize(listHandler), count));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    public class tblCountryHandler
    {
        public int CountryID { get; set; }
        public string CountryName {get;set;}
    }
}